#!/bin/bash
DATE=$(date +%Y%m%d%H%M)
DEPLOY_DIR=/Program
JAR_DIR=${DEPLOY_DIR}/webapps
BACKUP_DIR=${DEPLOY_DIR}/backup
LOG_DIR=${DEPLOY_DIR}/logs
LOG_FILE=${LOG_DIR}/report.logs
JAR_NAME=ReportService-0.0.1-SNAPSHOT

export JAVA_HOME PATH CLASSPATH
JAVA_HOME=/home/cmcc/soft/jdk1.8.0_131
PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH


if [ ! -d $DEPLOY_DIR ];then
   mkdir -p $DEPLOY_DIR
fi

if [ ! -d $JAR_DIR ];then
   mkdir -p $JAR_DIR
fi

if [ ! -d $BACKUP_DIR ];then
   mkdir -p $BACKUP_DIR
fi

if [ ! -d $LOG_DIR ];then
   mkdir -p $LOG_DIR
fi

if [ ! -f $LOG_FILE ];then
   touch   $LOG_FILE
fi

#kill -9 `ps -ef | grep $JARNAME | grep -v grep | awk '{print $2}'`
#sleep  5s
fuser  -k  ${LOG_FILE}
sleep  5s

cd $DEPLOY_DIR

mv -f  ${JAR_DIR}/ReportService-0.0.1-SNAPSHOT.jar    ${BACKUP_DIR}/ReportService-0.0.1-SNAPSHOT.jar"${DATE}"

mv -f /root/ReportService/target/ReportService-0.0.1-SNAPSHOT.jar   $JAR_DIR

cd $JAR_DIR
nohup java -jar ReportService-0.0.1-SNAPSHOT.jar  --spring.cloud.config.profile=dev  >${LOG_FILE} 2>&1 &
if [ $? = 0 ];then
        sleep 30
        tail -n 50 ${LOG_FILE}
fi

cd $BACKUP_DIR
ls -lt |grep ${JAR_NAME}| awk  'NR>5{print $NF}' |xargs rm -rf
